import React from 'react'
import BaseFilter, {filterCodes} from "../Base/BaseFilter";
import {findIndex, isArray, isObject, map} from "lodash-es";
import BaseFormItem from "../Base/BaseFormItem";
import {FinderSelectBase} from "./index";
import FinderRangeBase from "./FinderRangeBase";

class FinderFilterBase extends BaseFilter {

    comboData = [
        filterCodes.eq, filterCodes.noEq, filterCodes.noNull, filterCodes.null
    ];

    getAjaxData(newValues = {}) {
        let reObj = super.getAjaxData(newValues);

        if (isObject(reObj.filterVal)) {
            let {compVal} = reObj;

            if (isArray(compVal)) {
                reObj.filterVal.value = map(compVal, (val) => (
                    isObject(val) ? val.id : val
                ));
            } else {
                reObj.filterVal.value = isObject(compVal) ? compVal.id : compVal;
                // reObj.filterVal.originValue = compVal;
            }
        }

        return reObj;
    }

    getCompProps(moreProps) {
        // 需要“介于”条件
        if (this.getPropsByName('needBetween') && -1 === findIndex(this.comboData, v =>
            v && v.code === filterCodes.between.code
        )) {
            this.comboData.push(filterCodes.between);
        }
        return moreProps;
    }

    compRender(moreProps = {}) {

        if (filterCodes.between.code === this.state.comboVal) { // 介于

            return (
                <FinderRangeBase {...moreProps} />
            );

        } else {
            return (<FinderSelectBase {...moreProps}/>);
        }

    }
}


export default class FinderSelectFilter extends BaseFormItem {
    Comp = FinderFilterBase;
}